package org.fei.ClassDiagram;

import org.fei.ClassDiagram.RunAction;
import org.fei.ClassDiagram.WindowComponents.ClassDiagramView;
import org.fei.ClassDiagram.WindowComponents.MessageJPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ui.OpenProjects;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.filesystems.FileObject;
import org.openide.util.NbBundle.Messages;
import java.net.URL;
import javax.swing.AbstractAction;
import javax.swing.SwingUtilities;
import org.fei.ClassDiagram.ProjectScanner;
import org.openide.awt.NotificationDisplayer.Priority;
import org.openide.util.RequestProcessor;


@ActionID(category = "Build",
id = "org.fei.ClassDiagram.GenerateDiagramAction")
@ActionRegistration(iconBase = "org/fei/ClassDiagram/ball.png",
displayName = "#CTL_GenerateDiagramAction")
@ActionReferences({
    @ActionReference(path = "Toolbars/ClassDiagramPlugin", position = 0)
})
@Messages("CTL_GenerateDiagramAction=Generate Class Diagram")
public final class GenerateDiagramAction extends AbstractAction implements ActionListener {

    // background thread pool
    private static final RequestProcessor RP = new RequestProcessor(GenerateDiagramAction.class);

    @Override
    public void actionPerformed(ActionEvent e) {
        // inicializacia referencie na otvorene projekty v IDE
        OpenProjects open = OpenProjects.getDefault();
        // ziskanie referencie na hlavny projekt
        Project mainProject = open.getMainProject();
        
        ProjectScanner ps = new ProjectScanner(mainProject);
        
        if (ps.isProjectValid()) {
            setEnabled(false);
            RP.post(new RunAction(this, mainProject, ps), 0, Thread.NORM_PRIORITY);
            //SwingUtilities.invokeLater(new RunAction(this, mainProject, ps));
        /*
            AntCompilator.run(ps.getBuildImplXml());
            
            // ziskanie adresara v ktorom su class subory
            ProjectPropertiesScanner propertiesScanner = new ProjectPropertiesScanner(ps.getProjectProperties().getPath());       
            FileObject classFilesFo = mainProject.getProjectDirectory().getFileObject(propertiesScanner.getClassesDir());
        
            ArrayList<String> classFiles = new ArrayList<String>();
            ps.findFiles(classFilesFo, "class", classFiles);
            
            ArrayList<String> sources = new  ArrayList<String>();
            ps.findFiles(mainProject.getProjectDirectory(), "java", sources);
            
            ArrayList<URL> urls = new ArrayList<URL>();
            ps.findFilesURL(classFilesFo, "class", urls);
            new ClassFileScanner(classFiles, urls, sources);*/
        }
        else {
            String[] messages = {"Your Main Project " + ps.getProjectName() + " is not supported project type.",
                                 "In this plugin version are supported only Java SE projects."};
            new ClassDiagramView(new MessageJPanel(messages), ps.getProjectName());
        }
    }
}
